package Utils.Concurrent;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

import Utils.Graphics.LoadingWheel;
import Utils.UI.UIUpdater;

/**
 * Loader is an AsyncTask used to handle the loading flow functionalities It
 * usually Receives a loading label (storing the loading information), a task
 * pool, and a specific task to load
 * 
 * 
 * @author micha
 * 
 */

public class Loader extends AsyncTask<String> {

	private String loadingType;
	private Label loadingLabel;
	private String beforFinishText;
	private TaskPool<String> runnerPool;

	@SuppressWarnings("unchecked")
	public Loader(Display display, String loadingType, Object... vals) {
		super(display, vals);
		this.loadingType = loadingType;

	}

	@SuppressWarnings("unchecked")
	@Override
	public String doInBackground() {

		if (loadingType.equals("transition")) {
			HandleTransitionLoading(vals);
		}

		if (loadingType.equals("wheel_transition")) {
			HandleWheelTransitionLoading(vals);
		}

		return null;
	}

	private void HandleWheelTransitionLoading(Object[] vals) {
		Button nextButton = (Button) vals[0];
		TaskPool<String> pool = (TaskPool<String>) vals[1];
		AsyncTask<String> task = (AsyncTask<String>) vals[2];
		LoadingWheel wheel = (LoadingWheel) vals[3];
		pool.getResults(task);
		wheel.stop();
		UIUpdater.setWidgetEnable(nextButton, true);
	}

	@SuppressWarnings("unchecked")
	private void HandleTransitionLoading(Object[] vals) {

		// get all objects from Loader constructor
		this.loadingLabel = (Label) vals[0];
		Button nextButton = (Button) vals[1];
		TaskPool<String> pool = (TaskPool<String>) vals[2];
		AsyncTask<String> task = (AsyncTask<String>) vals[3];
		this.beforFinishText = (String) vals[4];
		String afterFinish = (String) vals[5];

		// set loading label visibility to be true <-----
		UIUpdater.setWidgetVisibile(loadingLabel, true);

		// create runner task
		LoaderRunner runner = new LoaderRunner(display);
		// add runner to runner pool
		this.runnerPool = new TaskPool<String>(1, false, runner);

		this.runnerPool.executeAll();
		pool.getResults(task);
		this.runnerPool.killAll(runner);
		UIUpdater.setWidgetText(loadingLabel, afterFinish);

		UIUpdater.setWidgetEnable(nextButton, true);
	}

	class LoaderRunner extends AsyncTask<String> {

		public LoaderRunner(Display display, Object... vals) {
			super(display, vals);
		}

		@Override
		public String doInBackground() {

			try {
				while (true) {

					UIUpdater.setWidgetText(loadingLabel, beforFinishText);
					Thread.sleep(210);
					UIUpdater
							.setWidgetText(loadingLabel, beforFinishText + ".");
					Thread.sleep(210);
					UIUpdater.setWidgetText(loadingLabel, beforFinishText
							+ "..");
					Thread.sleep(210);
					UIUpdater.setWidgetText(loadingLabel, beforFinishText
							+ "...");
					Thread.sleep(210);

				}
			} catch (Exception e) {
				// System.out.println(e);
			}
			return null;

		}

	}
}
